﻿using S.Weaning.Domain;
using S.Weaning.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.Weaning.Infrastructure.Impl
{
    public class LambAblactationRepository : BaseRepository<LambAblactationModel>, ILambAblactationRepository
    {
        private readonly MyDbContext db;

        public LambAblactationRepository(MyDbContext db) : base(db)
        {
            this.db = db;
        }
        /// <summary>
        /// 羔羊断奶新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int AddLambAblactation(List<LambAblactationModel> model)
        {
            db.LambAblactationModel.AddRange(model);
            int rowsAffected = db.SaveChanges();

            return rowsAffected;
        }
        /// <summary>
        /// 羔羊显示
        /// </summary>
        /// <param name="ddate"></param>
        /// <param name="ear"></param>
        /// <param name="dayage"></param>
        /// <param name="weight"></param>
        /// <param name="maternal"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<LambAblactationModel> GetAllLambAblactations(string? ddate, string? ear, int dayage, decimal? weightmin,decimal? weightgmax, string? maternal)
        {
            var query = db.LambAblactationModel.AsQueryable();

            if (!string.IsNullOrEmpty(ddate))
            {

                DateTime searchDate = DateTime.Parse(ddate);
                query = query.Where(t => t.WeaningDate.HasValue && t.WeaningDate.Value.Date == searchDate.Date);
            }
            if (!string.IsNullOrEmpty(ear))
            {
                query = query.Where(t => t.EITag.Contains(ear));
            }
            if (dayage != 0)
            {
                query = query.Where(t => t.WeaningDayAge == dayage);
            }
            if (weightmin.HasValue)
            {
                query = query.Where(t => t.WeaningWeight >= weightmin);
            }
            if (weightgmax.HasValue)
            {
                query = query.Where(t =>  t.WeaningWeight < weightgmax);
            }
            if (!string.IsNullOrEmpty(maternal))
            {
                query = query.Where(t => t.MammalianNumber.Contains(maternal));
            }

            var list = query.ToList();
            return list;
        }
    }
}
